package com.bywlkjs.common.util;

import lombok.Getter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 用来快速读取去Cookie
 * 实例化时需要明确给定一个Cookie数组
 *  @author 方某人
 */
@Getter
public class CookieUtil{
    /**所有cookie的list集合*/
    private List<Cookie> cookieList;
    private HashMap<String,String> map;

    public CookieUtil(Cookie[] cookies) {
        this.cookieList =Arrays.asList(cookies);
        this.map=new HashMap<>();
        for (Cookie cookie : cookies) {
            map.put(cookie.getName(),cookie.getValue());
        }
    }

    /**
     * 添加Cookie
     * @param key
     * @param value
     */
    public void put(String key, String value) {
        put(key,value);
        cookieList.add(new Cookie(key,value));
    }

    /**
     * 根据key来读取cookie
     * @param key
     * @return
     */
    public String get(String key){
        return map.get(key);
    }

}
